package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.GetThreadLocal;
import com.itheima.common.R;
import com.itheima.pojo.AddressBook;
import com.itheima.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/11/7 11:29
 */
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 添加地址
     * @return
     */
    @PostMapping
    public R address(@RequestBody AddressBook addressBook){
        Long userId = GetThreadLocal.getThreadLocal();
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }

    /**
     *
     * 修改地址数据回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R updateAddressData(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);

    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("修改成功！");
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R deleteAddressBook(Long ids){
        addressBookService.removeById(ids);
        return R.success("删除成功！");
    }
    
    /**
     * 获取地址列表
     * @return
     */
    @GetMapping("/list")
    public R getAddressBook(){
        // 从ThreadLocal中获取
        Long userId = GetThreadLocal.getThreadLocal();
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId, userId);
        List<AddressBook> addressBookList = addressBookService.list(queryWrapper);
        return R.success(addressBookList);
    }
    
    
    /**
     * 设置为默认地址
     * @return
     */
    @PutMapping("/default")
    public R setDefault(@RequestBody AddressBook addressBook) {
        // 先将该用户所有的默认地址设置为0，再将指定的id设置为默认地址
        Long userId = GetThreadLocal.getThreadLocal();
        LambdaQueryWrapper<AddressBook> querryWrapper = new LambdaQueryWrapper<>();
        querryWrapper.eq(AddressBook::getUserId, userId);
        addressBook.setIsDefault(0);
        addressBookService.update(addressBook, querryWrapper);

        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置成功！");
    }

    @GetMapping("/default")
    public R getDefault(){
        Long userId = GetThreadLocal.getThreadLocal();
        LambdaQueryWrapper<AddressBook> querryWrapper = new LambdaQueryWrapper<>();
        querryWrapper.eq(AddressBook::getUserId, userId);
        querryWrapper.eq(AddressBook::getIsDefault, 1);
        AddressBook defaultAddressBook = addressBookService.getOne(querryWrapper);
        return R.success(defaultAddressBook);
    }
}
